<?php



namespace app\controller\admin\system\common;


use app\common\repositories\system\common\ProblemCategoryRepository;
use app\validate\admin\ArticleValidate;
use crmeb\basic\BaseController;
use app\validate\admin\CommonProblemValidate;
use think\App;
use app\common\repositories\system\common\ProblemRepository as repository;

class Problem extends BaseController
{
    /**
     * @var \app\common\model\system\common\ProblemCategory
     */
    protected $repository;

    /**
     * User constructor.
     * @param App $app
     * @param  $repository
     */
    public function __construct(App $app, repository $repository)
    {
        parent::__construct($app);
        $this->repository = $repository;
    }

    /**
     * @return mixed
     * @author Qinii
     */
    public function lst()
    {
        $where = $this->request->params(['keyword', 'type', 'is_show','title']);
        [$page, $limit] = $this->getPage();
        return app('json')->success($this->repository->getList($where, $page, $limit));
    }



    /**
     * 添加
     * @param CommonProblemValidate $validate
     * @param ProblemCategoryRepository $repository
     * @return mixed
     * @author Qinii
     */
    public function create(CommonProblemValidate $validate,ProblemCategoryRepository $repository)
    {
        $data = $this->checkParams($validate);
        if (!$repository->exists($data['common_problem_type_id']))
            return app('json')->fail('分类不存在');
        $this->repository->create($data);
        return  app('json')->success('添加成功');

    }

    /**
     * 更新
     * @param $id
     * @param CommonProblemValidate $validate
     * @param ProblemCategoryRepository $articleCategoryRepository
     * @return mixed
     * @author Qinii
     */
    public function update($id,CommonProblemValidate $validate,ProblemCategoryRepository $articleCategoryRepository)
    {
        $data = $this->checkParams($validate);
        if (!$this->repository->exists($id))
            return app('json')->fail('数据不存在');
        if (!$articleCategoryRepository->exists($data['common_problem_type_id']))
            return app('json')->fail('分类不存在');

        $this->repository->update($id,$data);

        return  app('json')->success('编辑成功');


    }

    /**
     * @param CommonProblemValidate $validate
     * @return array
     * @author Qinii
     */
    public function checkParams(CommonProblemValidate $validate)
    {
        $data = $this->request->params([['common_problem_type_id', 0], 'title', 'content', 'is_show','sort']);
        $validate->check($data);
        return $data;
    }

    public function switchStatus($id)
    {
        $status = $this->request->param('status', 0) == 1 ? 1 : 0;
        $this->repository->switchStatus($id, $status);
        return app('json')->success('修改成功');
    }

    public function detail($id)
    {
        if (!$this->repository->fieldExists('common_problem_id',$id))
            return app('json')->fail('数据不存在');

        return app('json')->success($this->repository->get($id));
    }


    /**
     * @param $id
     * @return mixed
     * @author Qinii
     */
    public function delete($id)
    {
        if (!$this->repository->fieldExists('common_problem_id',$id))
            return app('json')->fail('数据不存在');
        $this->repository->delete($id);
        return app('json')->success('删除成功');
    }
}
